/*
*/
ALTER PROC SP_SAVE_FORECAST_ADDITONAL_DATA ( 
					@PROJID INT,
					@PRODID INT,
					@YEAR INT,
					@QTR INT,
					@REBATE FLOAT,
					@MATERIAL_COST FLOAT,
					@ROYALTY FLOAT,
					@PORT_VOLUME FLOAT,
					@TRANSACTION_ID VARCHAR(50))
AS
BEGIN
	DECLARE @ROW_ID INT
	SET @ROW_ID = NULL
	

	IF( (SELECT COUNT(*) FROM FCENTRYMASTER_ADDITIONAL_DATA_TABLE WHERE PROJID = @PROJID AND PRODID = @PRODID AND [QUARTER] = @QTR AND [YEAR] = @YEAR) = 0 ) --FORECAST NOT EXIST FOR THIS PROJ, PROD, QUARTER, YEAR
	 BEGIN
		INSERT INTO FCENTRYMASTER_ADDITIONAL_DATA_TABLE ( PROJID, PRODID, [QUARTER], YEAR, REBATE, MATERIAL_COST, ROYALTY, PORT_VOLUME, TRANSACTION_ID )
			VALUES (@PROJID, @PRODID, @QTR, @YEAR, @REBATE, @MATERIAL_COST, @ROYALTY, @PORT_VOLUME, @TRANSACTION_ID)			
	 END
	ELSE
	 BEGIN
/*
		--copy to history table
		INSERT INTO FCENTRYMASTER_ADDITIONAL_DATA_HISTORY_TABLE ( PROJID, PRODID, [QUARTER], YEAR, REBATE, MATERIAL_COST, ROYALTY, PORT_VOLUME, TRANSACTION_ID ) 
			( SELECT PROJID, PRODID, [QUARTER], YEAR, REBATE, MATERIAL_COST, ROYALTY, PORT_VOLUME, TRANSACTION_ID FROM FCENTRYMASTER_ADDITIONAL_DATA_TABLE WHERE PROJID = @PROJID AND PRODID = @PRODID AND [QUARTER] = @QTR AND [YEAR] = @YEAR )
*/		
		--now update
		UPDATE FCENTRYMASTER_ADDITIONAL_DATA_TABLE 
			SET REBATE = @REBATE, 
				MATERIAL_COST = @MATERIAL_COST,
				ROYALTY = @ROYALTY, 
				PORT_VOLUME = @PORT_VOLUME,
				TRANSACTION_ID = @TRANSACTION_ID
			WHERE PROJID = @PROJID AND PRODID = @PRODID AND [QUARTER] = @QTR AND [YEAR] = @YEAR
	 END	
END